<?php
namespace app\modules\api\service;

use app\modules\api\models\Income;
use app\modules\api\models\Bank_bill;
use app\modules\api\models\Income_type;
use app\modules\api\models\IncomeProperty;
use app\modules\api\models\Staff;
use app\modules\api\models\Bank_type;
use app\modules\api\models\Department;
use app\modules\api\models\Invoice_new;

class ReportService
{
    /**
     * 报表收入列表
     */
    public function income($p, $s, $where = [])
    {
        $db = new Income();
        $list = $db->income_list($p, $s, $where);
        return $list;
    }

    /**
     * 报表大小
     */
    public function income_size($where = [])
    {
        $db = new Income();
        return $size = $db->income_size($where);
    }

    /**收入报表通用信息
     * @return array
     */
    public function incomeBase()
    {

        $data = [];
        //员工
        $staff = new Staff();
        $data['staff'] = $staff->list();
        //收入类型
        $incomeType = new Income_type();
        $data['incomeType'] = $incomeType->get_type();
        //性质
        $incomeProperty = new IncomeProperty();
        $data['incomeProperty'] = $incomeProperty->list();
        return $data;
    }

    /**
     * 更新income
     * @param array $data
     * @return bool
     */
    public function update($data)
    {
        $model = new Income();
        return $model->update($data);
    }

    /**
     * 银行报表
     */
    public function bank_list($p, $s, $where = [])
    {
        $model = new Bank_bill();
        return $model->list($p, $s, $where);
    }

    /**
     * @return array
     */
    
    public function bank_type():array
    {
        $data = [];
        $model = new Staff();
        $data['staff'] = $model->list();
        $bankTypeModel = new Bank_type();
        $data['type'] = $bankTypeModel->list();
        $departModel = new Department();
        $data['group'] = $departModel->list();
        return $data;
    }

    /**
     * 银行报表size
     * @return int
     */
    public function bank_size($where = []):int
    {
        $model = new Bank_bill();
        return $model->size($where);
    }

    /**
     * 银行报表数据计算
     */
    public function bankcaculate($where = [])
    {
        $model = new Bank_bill();
        return $model->caculate($where);
    }

    /**
     * 订单汇总
     */
    public function order($p, $s, $where = [])
    {
        $model = new Invoice_new();
        $list = [];
        $list['list'] = $model->list($p, $s, $where);
        $invoice_arr = [];
        foreach ($list['list'] as $v) {
            $invoice_arr[] = $v['invoice'];
        }

        foreach ($list['list'] as $k => $v) {
            $invoice_arr[] = $v['invoice'];
            // foreach ($list['income'] as $k2 => $v2) {
            //     if (trim($v['invoice']) == trim($v2['invocie_num'])) {
            //         $list['list'][$k]['income'] = $v2;
            //         $list['list'][$k]['date2'] = $v2['date'];
            //         unset($list['income'][$k2]);
            //         break;
            //     }
            // }
        }
        unset($list['income']);
        //invoice添加运费
        $express_model = new \app\models\Express();
        $express_list = $express_model->get_all_express_money($invoice_arr);

        //invoice添加仓库费用
        $repo_model = new \app\models\Repo();
        $repofee_list = $repo_model->get_repo_fee_by_invoice($invoice_arr);

        //打印费用添加
        $print_model = new \app\models\PrintModel();
        $print_list = $print_model->get_print_fee_by_invoice($invoice_arr);

        //RMB收入
        $bank_bill = new \app\models\Bank_bill();
        $bank_list = $bank_bill->get_money_by_invoice($invoice_arr);

        //添加汇率
        $commonModel = new \app\models\CommonModel();
        $exchange_list = json_decode($commonModel->get_all_exchange_rate(), true);

        //内陆费用添加
        $inlandModel = new \app\models\Inland();
        $inland_list = $inlandModel->get_inland_list_by_invoice($invoice_arr);

        foreach ($list['list'] as $k => $v) {
            foreach ($express_list as $v2) {
                if ($v['invoice'] == $v2['invoice_order']) {
                    $list['list'][$k]['express_fee'] = $v2['fee'];
                    break;
                }
            }

            foreach ($repofee_list as $v2) {
                if ($v['invoice'] == $v2['invoice_order']) {
                    $list['list'][$k]['repo_fee'] = $v2['pay_all'];
                    break;
                }
            }

            foreach ($print_list as $v2) {
                if ($v['invoice'] == $v2['invoice_order']) {
                    $list['list'][$k]['print_fee'] = $v2['fee'];
                    break;
                }
            }

            foreach ($bank_list as $v2) {
                if ($v['invoice'] == $v2['invocie_num']) {
                    $list['list'][$k]['bank'] = $v2['in_money'] - $v2['out_money'];
                    break;
                }
            }

            foreach ($exchange_list as $v2) {
                $date  = isset($v['date2'])?$v['date2']:$v['date'];
                if (date('Ym', strtotime($date)) == $v2['month']) {
                    $list['list'][$k]['exchange_rate'] = $v2['rate'];
                    break;
                }
            }

            foreach ($inland_list as $v2) {
                if ($v['invoice'] == $v2['invoice']) {
                    $list['list'][$k]['inland_fee'] = $v2['total_fee'];
                }
            }
        }
        return $list;
    }

    /**
     * 订单汇总order size
     * @param array $where
     * @return int 
     */
    public function order_size($where = [])
    {
        $model = new Invoice_new();
        return  $model->size($where);
    }
}
